<!DOCTYPE html>
<html>
<head>
    <meta name="layout" content="main" />
    <g:set var="entityName" value="${message(code: 'jasperFile.label', default: 'jasperFile')}" />
    <title>编辑下载模板</title>
    <asset:stylesheet src="homer/vendor/jQuery-File-Upload/css/jquery.fileupload.css"/>
    <asset:stylesheet src="homer/vendor/blueimp-gallery/css/blueimp-gallery.min.css"/>

    <style>

    .img {
        width: 80px;
        height: 58px;
    }

    .fileupload table > tbody > tr > td {
        vertical-align: middle;
    }
    .fileupload-buttonbar button,.files button{
        margin-top: 2px;
    }
    </style>
</head>
<body>
<div class="small-header">
    <div class="hpanel">
        <div class="panel-body">
            <div id="hbreadcrumb" class="pull-right">
                <ol class="hbreadcrumb breadcrumb">
                    <li>贷款管理系统</li>
                    <li>
                        <g:link controller="jasperFile" action="index">下载模板管理</g:link>
                    </li>
                    <li class="active">
                        修改下载模板
                    </li>
                </ol>
            </div>
            <h2 class="font-light m-b-xs" style="text-transform:none;">
                修改下载模板
            </h2>
        </div>
    </div>
</div>
<div class="content animate-panel">
    <div class="row">
        <div class="hpanel hblue">
            <div class="panel-heading">
                修改下载模板
            </div>

            <div class="panel-body">
                <div id="edit-jasperFile" class="content scaffold-edit" role="main">
                    <g:if test="${flash.message}">
                        <div class="message" role="status">${flash.message}</div>
                    </g:if>
                    <g:hasErrors bean="${this.jasperFile}">
                        <ul class="errors" role="alert">
                            <g:eachError bean="${this.jasperFile}" var="error">
                                <li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
                            </g:eachError>
                        </ul>
                    </g:hasErrors>
                    <form method="post" action="http://upload-z1.qiniup.com" class="fileupload2 form-horizontal"
                          enctype="multipart/form-data">
                        <div class="form-group">
                            <label class="col-md-2 control-label">下载模板</label>
                            <div class="col-md-8">
                                <div class="row fileupload-buttonbar">
                                    <div class="col-lg-8">
                                        <!-- The fileinput-button span is used to style the file input field as button -->
                                        <span class="btn btn-info fileinput-button">
                                            <i class="glyphicon glyphicon-plus"></i>
                                            <span>选择文件</span>
                                            <input name="x:description" class="description" value="">
                                            <input name="token" class="token" value="">
                                            <input name="key" class="key" value="">
                                            <input type="file" name="file" id="file2" multiple>
                                        </span>
                                        <button type="submit" class="btn btn-primary start">
                                            <i class="glyphicon glyphicon-upload"></i>
                                            <span>开始上传</span>
                                        </button>
                                        <button type="reset" class="btn btn-warning cancel">
                                            <i class="glyphicon glyphicon-ban-circle"></i>
                                            <span>取消上传</span>
                                        </button>

                                        <span class="fileupload-process"></span>
                                    </div>
                                    <!-- The global progress state -->
                                    <div class="col-lg-4 fileupload-progress fade">
                                        <!-- The global progress bar -->
                                        <div class="progress full progress-striped active" role="progressbar"
                                             aria-valuemin="0" aria-valuemax="100">
                                            <div class="progress-bar progress-bar-success" style="width:0%;"></div>
                                        </div>
                                        <!-- The extended global progress state -->
                                        <div class="progress-extended">&nbsp;</div>
                                    </div>
                                </div>
                                <!-- The table listing the files available for upload/download -->
                                <p style="margin-top: 10px;">
                                    <code>附件大小不能超过 15m,附件格式jrxml</code>
                                </p>
                                <table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>
                            </div>
                        </div>
                    </form>
                    <div class="hr-line-dashed"></div>
                    <g:form resource="${this.jasperFile}" method="PUT" class="form-horizontal jasperFileForm">
                        <div class="form-group">
                            <label class="col-md-3 control-label">模板名称</label>

                            <div class="col-md-3">
                                <g:textField type="text" name="name" id="name" value="${this.jasperFile?.name}" class="form-control"/>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">模板描述</label>

                            <div class="col-md-3 input-group">
                                <g:textField type="text" name="description" id="description" value="${this.jasperFile?.description}" class="form-control"/>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">模板名称</label>
                            <div class="col-md-3">
                                <g:select class="form-control" name="component.id" required="required" id="component"
                                          value="${this.jasperFile?.component?.id}" from="${com.next.Component.list()}"
                                          optionKey="id" optionValue="name"></g:select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">模板路径</label>
                            <div class="col-md-3">
                                <g:textField type="text" name="fileUrl" id="fileUrl" value="${this.jasperFile?.fileUrl}" class="form-control" readOnly="true"/>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-md-3 col-lg-offset-3">
                                <g:submitButton class="btn btn-info" name="create" value="保存" id="submitButton"
                                                type="button"/>
                            </div>
                        </div>
                    </g:form>

                </div>
            </div>
        </div>
    </div>
</div><script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-upload fade">
       %{-- <td>
            <span class="preview"></span>
        </td>--}%
        <td>
            <p class="name">{%=file.name%}</p>
            <strong class="error text-danger"></strong>
        </td>
        <td>
            <p class="size">Processing...</p>
            <div class="progress full progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>
        </td>
        <td>
            {% if (!i && !o.options.autoUpload) { %}
                <button class="btn btn-primary start" disabled>
                    <i class="glyphicon glyphicon-upload"></i>
                    <span>上传</span>
                </button>
            {% } %}
            {% if (!i) { %}
                <button class="btn btn-warning cancel">
                    <i class="glyphicon glyphicon-ban-circle"></i>
                    <span>取消</span>
                </button>
            {% } %}
        </td>
    </tr>
{% } %}
</script>
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-download fade">
        <td>
            <span class="preview">
                {% if (file.thumbnailUrl) { %}
                    <a id="thumbnailUrl" href="{%=file.thumbnailUrl%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery></a>
                {% } %}
            </span>
        </td>
        <td>
            <p class="name lightBoxGallery">
                {% if (file.url) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>{%=file.name%}</a>
                {% } else { %}
                    <span>{%=file.name%}</span>
                {% } %}
            </p>
            {% if (file.error) { %}
                <div><span class="label label-danger">Error</span>{%=file.error%}</div>
            {% } %}
        </td>
        <td>
            <span class="size">{%=file.size%}</span>
        </td>
        <td>
         {% if (file.error) { %}
          <button class="btn btn-success cancel">
                    <i class="glyphicon glyphicon-ban-circle"></i>
                    <span>取消上传</span>
                </button>
          {% } else { %}
               <button class="btn btn-success" type="button">
                    <i class="glyphicon glyphicon-ok"></i>
                    <span>上传成功</span>
               </button>
          {% } %}
        </td>
    </tr>
    {% var fileName = o.file[0].thumbnailUrl;
    $("#fileUrl").val(fileName); %}
{% } %}
</script>
<asset:javascript src="homer/vendor/jQuery-File-Upload/js/vendor/jquery.ui.widget.js"/>
<asset:javascript src="homer/vendor/jQuery-File-Upload/js/tmpl.min.js"/>
<asset:javascript src="homer/vendor/jQuery-File-Upload/js/load-image.all.min.js"/>
<asset:javascript src="homer/vendor/jQuery-File-Upload/js/canvas-to-blob.min.js"/>

<asset:javascript src="homer/vendor/jQuery-File-Upload/js/jquery.iframe-transport.js"/>
<!-- The basic File Upload plugin -->
<asset:javascript src="homer/vendor/jQuery-File-Upload/js/jquery.fileupload.js"/>
<!-- The File Upload processing plugin -->
<asset:javascript src="homer/vendor/jQuery-File-Upload/js/jquery.fileupload-process.js"/>
<!-- The File Upload image preview & resize plugin -->
<asset:javascript src="homer/vendor/jQuery-File-Upload/js/jquery.fileupload-image.js"/>
<!-- The File Upload validation plugin -->
<asset:javascript src="homer/vendor/jQuery-File-Upload/js/jquery.fileupload-validate.js"/>
<!-- The File Upload user interface plugin -->
<asset:javascript src="homer/vendor/jQuery-File-Upload/js/jquery.fileupload-ui.js"/>
<!-- The main application script -->
<asset:javascript src="homer/vendor/jQuery-File-Upload/js/cors/jquery.xdr-transport.js"/>
<asset:javascript src="homer/vendor/blueimp-gallery/js/jquery.blueimp-gallery.min.js"/>

<script>
    $(function () {
        $("#submitButton").click(function () {
            $("#fileUrl").val($("#thumbnailUrl").attr("href"));
            $(".jasperFileForm").submit();

        });

        $('#type option').each(function () {
            var obj = $(this);
            var text = obj.text();
            $(this).attr('text', text);
        });
        $('#file2').change(function ()
        {
            var tokenTxt = $(this).siblings(".token");
            var description = $(this).siblings(".description");
            var key = $(this).siblings(".key");
            tokenTxt.val(getUploadToken());

            var files = document.getElementById('file2');
            for (var i = 0; i < files.files.length; i++)
            {
                var file = files.files[i];
                description.val(file.name);
                key.val(getRandom(28) + file.name.substr(file.name.lastIndexOf('.')))
            }
        });
        $('.fileupload2').fileupload({
            messages: {
                maxNumberOfFiles: '文件个数不能超过1个',
            },
            maxFileSize: 2560000,
            maxNumberOfFiles: 1,
            acceptFileTypes: /(\.|\/)(jrxml)$/i
        });
        function getUploadToken() {
            var token = ""
            $.ajax({
                type: "POST",
                url: "http://" + window.location.host + "/jasperFile/getUploadToken",
                async:false,
                success: function (data) {
                    if (data.status == "success") {
                        token = data.token;
                        console.log("getUploadToken success.");
                    }
                },
            });
            return token
        };

        function getRandom(length)
        {
            var str = Math.random().toString(36).substr(2);
            if (str.length>=length) {
                return str.substr(0, length);
            }
            str += getRandom(length-str.length);
            return str;
        };
    });
</script>
</body>
</html>
